package net.caixiaomi.info.adapter;

import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.AccountDetailItem;

/**
 * 优惠券
 */

public class AccountDetailsAdapter extends BaseQuickAdapter<AccountDetailItem, BaseViewHolder> {

    public AccountDetailsAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, AccountDetailItem item) {
        try {
            int adapterPosition = helper.getAdapterPosition();
            if (adapterPosition - 1 >= 0) {
                AccountDetailItem previousItem = getData().get(adapterPosition - 1);
                helper.setGone(R.id.time, !TextUtils.equals(item.getAddTime(), previousItem.getAddTime()));
            } else {
                helper.setGone(R.id.time, true);
            }
            helper.setText(R.id.time, item.getAddTime());
            helper.setText(R.id.title, item.getProcessTypeName());
//            helper.setText(R.id.price, item.getChangeAmount());
            TextView mPrice = helper.getView(R.id.price);
            mPrice.setText(item.getChangeAmount());
            mPrice.setCompoundDrawablesWithIntrinsicBounds(null, null, TextUtils.equals(item.getProcessType(), "4") ? ContextCompat.getDrawable(CommonApp.getInstance(), R.mipmap.ic_greylight_arrow_right) : null, null);
            helper.setText(R.id.type, item.getProcessTypeChar());
            int background = R.drawable.bg_consume;
            int priceTextColor = R.color.primary_text;
            if (TextUtils.equals(item.getProcessType(), "1") || TextUtils.equals(item.getProcessType(), "2") || TextUtils.equals(item.getProcessType(), "5") || TextUtils.equals(item.getProcessType(), "6")) {
                background = R.drawable.bg_income;
                if (TextUtils.equals(item.getProcessType(), "6")) {
                    background = R.drawable.bg_refund;
                }
                priceTextColor = R.color.orange_primary;
            }
            mPrice.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), priceTextColor));
            helper.getView(R.id.type).setBackgroundResource(background);

            helper.setText(R.id.status, item.getStatus());
            StringBuilder stringBuilder = new StringBuilder();
            if (!TextUtils.isEmpty(item.getNote())) {
                stringBuilder.append(item.getNote()).append("\n");
            }
            stringBuilder.append(item.getShotTime());
            helper.setText(R.id.info, stringBuilder);

            if (adapterPosition + 1 <= getData().size() - 1) {
                AccountDetailItem nextItem = getData().get(adapterPosition + 1);
                helper.setGone(R.id.line, TextUtils.equals(item.getAddTime(), nextItem.getAddTime()));
            } else {
                helper.setGone(R.id.line, false);
            }
            helper.setVisible(R.id.btn_status, !TextUtils.isEmpty(item.getStatus()));
            helper.addOnClickListener(R.id.btn_status);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
